package cn.quickcode.monitor.ui.dialog;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.ResourceManager;

import cn.quickcode.monitor.model.Ntm;
import cn.quickcode.monitor.ui.part.NTMViewPart;

public class NewNTMDialog extends Dialog
{
	public ViewPart viewPart;
	public Label lblNameLabel;
	public Text txtName;
	/**
	 * @wbp.parser.constructor
	 */
	public NewNTMDialog(Shell parentShell,ViewPart viewPart)
	{
		super(parentShell);
		this.viewPart = viewPart;
	}
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	private NewNTMDialog(Shell parentShell)
	{
		super(parentShell);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
	 */
	@Override
	protected void configureShell(Shell newShell)
	{
		// TODO Auto-generated method stub
		super.configureShell(newShell);
		newShell.setText("添加监控");
		newShell.setImage(ResourceManager.getPluginImage("SuperMonitor", "icons/icon0.gif"));
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent)
	{
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gl_container = new GridLayout(4, false);
		gl_container.marginRight = 10;
		gl_container.marginLeft = 10;
		gl_container.marginTop = 10;
		container.setLayout(gl_container);
		
		this.lblNameLabel = new Label(container, SWT.NONE);
		this.lblNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		this.lblNameLabel.setText("名称：");
		
		this.txtName = new Text(container, SWT.BORDER);
		this.txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		this.lblUrlLabel = new Label(container, SWT.NONE);
		this.lblUrlLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		this.lblUrlLabel.setText("地址：");
		
		this.txtIp = new Text(container, SWT.BORDER);
		this.txtIp.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		return container;
	}
	public static final int BTN_OK_ID=1;
	public static final int BTN_CANCEL_ID=0;
	public static final int BTN_TEST_ID=2;
	public static final String BTN_OK_LABEL="OK";
	public static final String BTN_CANCEL_LABEL="Cancel";
	public static final String BTN_TEST_LABLE="Test";
	public Label lblUrlLabel;
	public Text txtIp;
	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent)
	{
		createButton(parent, BTN_TEST_ID, BTN_TEST_LABLE, true);
		createButton(parent, BTN_OK_ID, BTN_OK_LABEL, true);
		createButton(parent, BTN_CANCEL_ID, BTN_CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize()
	{
		return new Point(450, 233);
	}
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
	 */
	@Override
	protected void buttonPressed(int buttonId)
	{
		if(BTN_OK_ID == buttonId)
		{
			Ntm ntm = new Ntm();
			ntm.setName(this.txtName.getText());
			ntm.setIp(this.txtIp.getText());
			if(Ntm.save(ntm))
			{
				((NTMViewPart)this.viewPart).refresh(null);
				this.close();
			}
			else
			{
				MessageDialog.openInformation(Display.getCurrent().getActiveShell(),"提示","操作失败，请重试！");
			}
		}
		else if(BTN_CANCEL_ID == buttonId)
		{
			this.close();
		}
	}
}
